//
//  ProgramViewController.m
//  EmilyCarrGradCatolog
//
//  Created by Miles on 10-03-25.
//  Copyright 2010 Miles Thorogood
//  greenmeat.net. All rights reserved.
//

#import "ProgramViewController.h"
#import "ArtistsForProgramViewController.h"

@implementation ProgramViewController
 
@synthesize managedObjectContext, selectedArtists ;

@synthesize bfaButton, bdeButton, bmeButton, maaButton ;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (IBAction) hitBFA {
	ArtistsForProgramViewController *programArtists = [[ArtistsForProgramViewController alloc] initWithNibName:@"ArtistsForProgramViewController" bundle:nil] ;
	
	[self programFetchController:@"BFA"] ;
	programArtists.artistList = selectedArtists ;
	programArtists.title = @"BFA" ;
	[self.navigationController pushViewController:programArtists animated:YES] ;
	[programArtists release] ;	
}

- (IBAction) hitMED {
	ArtistsForProgramViewController *programArtists = [[ArtistsForProgramViewController alloc] initWithNibName:@"ArtistsForProgramViewController" bundle:nil] ;
	
	[self programFetchController:@"BME"] ;
	programArtists.artistList = selectedArtists ;
	programArtists.title = @"BME" ;
	[self.navigationController pushViewController:programArtists animated:YES] ;
	[programArtists release] ;	
}
- (IBAction) hitBDE {
	ArtistsForProgramViewController *programArtists = [[ArtistsForProgramViewController alloc] initWithNibName:@"ArtistsForProgramViewController" bundle:nil] ;
	
	[self programFetchController:@"BDE"] ;
	programArtists.artistList = selectedArtists ;
	programArtists.title = @"BDE" ;
	[self.navigationController pushViewController:programArtists animated:YES] ;
	[programArtists release] ;	
}

- (IBAction) hitMAA {
	ArtistsForProgramViewController *programArtists = [[ArtistsForProgramViewController alloc] initWithNibName:@"ArtistsForProgramViewController" bundle:nil] ;
	
	[self programFetchController:@"MAA"] ;
	programArtists.artistList = selectedArtists ;
	programArtists.title = @"MAA" ;
	[self.navigationController pushViewController:programArtists animated:YES] ;
	[programArtists release] ;	
}

- (NSMutableArray *) programFetchController:(NSString *) program {

	NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:managedObjectContext];
	[fetchRequest setEntity:entity];
	
	 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"program contains [c] %@", program];
	 
	 //NSLog( @"predicate = %@", predicate ); 
	 [fetchRequest setPredicate:predicate] ;
	
	// Order the events by creation date, most recent first.
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:NO];
	NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
	[fetchRequest setSortDescriptors:sortDescriptors];
	[sortDescriptor release];
	[sortDescriptors release];
	
	NSError *error = nil;
	NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
	if (mutableFetchResults == nil) {
		NSLog(@"Error fetching results");
	}

	selectedArtists = [mutableFetchResults mutableCopy] ;
	
	return mutableFetchResults ;
	//[mutableFetchResults release];
	

}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
